package com.hgkj.cloud.base.scheme.table;

import com.hgkj.cloud.base.scheme.ClsAnswerRequest;
import com.hgkj.cloud.base.scheme.ClsAnswerResult;
import com.hgkj.cloud.base.scheme.ClsScore;

import java.util.Map;

/**
 * 十八、汉密顿抑郁量表（Hamd）共24题
 */
public class Table18 extends AbstractTable implements TableCalculate{
    String code = "Hamd";

    @Override
    public String getCode() {
        return code;
    }

    @Override
    public ClsAnswerResult calc(ClsAnswerRequest answer) {
        return GetHamdFactorScore(answer);
    }

    @Override
    public String[] report(Map map) {
        return null;
    }

    private static ClsAnswerResult GetHamdFactorScore(ClsAnswerRequest answer) {
        // 1总粗分，2焦虑/躯体化，3体重，4认识障碍，5日夜变化，6阻滞，7睡眠障碍，8绝望感，9全身症状
        ClsAnswerResult result = new ClsAnswerResult();
        result.setMessage("");
        result.setType(answer.getType());
        result.setUuid(answer.getUuid());
        try {
            // 1正确题数
            int MAX_NUM = 24;

            int intI;
            String[] strAnswer_c = answer.getAnswer().split(g_sChar);
            if (strAnswer_c.length > 1) {
                for (intI = 1; intI < strAnswer_c.length; intI++) {
                    strAnswer_c[intI] = strAnswer_c[intI].trim();
                }
            }

            String[] sAnswer = strAnswer_c[0].toString().split(g_nChar);

            if (sAnswer.length != MAX_NUM) {
                result.setMessage("答案数量不符！");
                return result;
            }

            int i, j;
            // 共一组0,1,2,3,4
            // “总粗分”为所有因子相加
            String[] strNumberA = { "10,11,12,15,17", "16", "2,3,9,19,20,21", "18", "1,7,8,14", "4,5,6", "22,23,24",
                    "13" };
            String[] strSubA;
            int[] vFactorA = new int[9]; // 实际为9个因子
            for (i = 0; i < vFactorA.length; i++) {
                vFactorA[i] = 0;
            }

            // 原始分
            for (i = 0; i < strNumberA.length; i++) {
                strSubA = strNumberA[i].split(",");
                for (j = 0; j < strSubA.length; j++) {
                    vFactorA[i + 1] += Integer.valueOf(sAnswer[Integer.valueOf(strSubA[j]) - 1]) - 1;
                }
                // 总粗分
                vFactorA[0] += vFactorA[i + 1];
            }

            // 按“总粗分”得到“诊断结果”
            // 按“总粗分”得到“指导意见”
            int[] intValueA = { 7, 19, 34 };
            String[] strValueA = { "没有抑郁症状", "可能有抑郁症状", "可能有中度的抑郁症状", "可能有严重抑郁症状" };
            String vAppraise = strValueA[strValueA.length - 1];
            String vDoctorIdea = "Hamd.1.4";
            for (i = 0; i < intValueA.length; i++) {
                if (vFactorA[0] <= intValueA[i]) {
                    vAppraise = strValueA[i];
                    vDoctorIdea = "Hamd.1." + String.valueOf(i + 1);
                    break;
                }
            }

            ClsScore.Hamd hamd = new ClsScore.Hamd();

            hamd.setTotal_o(vFactorA[0]);
            hamd.setTotal_a(vAppraise);
            hamd.setTotal_d(vDoctorIdea);

            hamd.setJl_o(vFactorA[1]);
            hamd.setTz_o(vFactorA[2]);
            hamd.setRsza_o(vFactorA[3]);
            hamd.setRybh_o(vFactorA[4]);
            hamd.setZz_o(vFactorA[5]);
            hamd.setSmza_o(vFactorA[6]);
            hamd.setJwg_o(vFactorA[7]);
            hamd.setQszz_o(vFactorA[8]);

            result.setObject(hamd);
            return result;
        } catch (ArithmeticException e) {
            result.setMessage(e.toString());
            return result;
        }
    }


}
